package com.lenovo.testreport.jenkins;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.*;
import java.net.*;

/**
 * @Descpription
 * @Author CJF
 * @Date 2019/1/16 17:33
 **/
public class DownloadFromJenkins {
    //jenkins登录账号
    public static String username = "lijie";
    //jenkins登录密码
    public static String password = "lenovo123456";

    static String url = "http://172.16.22.105/jenkins/job/cloud/job/client/job/Android/job/publiccloud-android-compile/ws/result/lenovoCloud-release.apk";

    public static void main(String[] args) throws IOException {
        downloadFile();
    }


    public static boolean downloadFile() {
        try {
            URI uri = URI.create(url);
            HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
            // Create AuthCache instance
            AuthCache authCache = new BasicAuthCache();
            // Generate BASIC scheme object and add it to the local auth cache
            BasicScheme basicAuth = new BasicScheme();
            authCache.put(host, basicAuth);
            CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
            HttpPost httpPost = new HttpPost(uri);
            // Add AuthCache to the execution context
            HttpClientContext localContext = HttpClientContext.create();
            localContext.setAuthCache(authCache);
            HttpResponse response = httpClient.execute(host, httpPost, localContext);
            BufferedInputStream br = new BufferedInputStream(response.getEntity().getContent());
            File file = new File("LenovoBox.apk");
            OutputStream out = new FileOutputStream(file);
            int size = 0;
            int len = 0;
            byte[] buf = new byte[1024];
            while ((size = br.read(buf)) != -1) {
                len += size;
                out.write(buf, 0, size);
                // 打印下载百分比
                // System.out.println("下载了-------> " + len * 100 / fileLength +
                // "%\n");
            }
            br.close();
            out.close();

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
